package com.zoe.onelink.circuitbreaker.sentinel;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.ClassUtils;

import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-01-17
 */
@Slf4j
public class SentinelCircuitBreakerEnvironmentPostProcessor implements EnvironmentPostProcessor {

    public static final String SENTINEL_CIRCUIT_BREAKER_AUTO_CONFIGURATION = "com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerAutoConfiguration";
    public static final String SENTINEL_CIRCUITBREAKER_ENABLED_KEY = "spring.cloud.circuitbreaker.sentinel.enabled";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        if (!ClassUtils.isPresent(SENTINEL_CIRCUIT_BREAKER_AUTO_CONFIGURATION, this.getClass().getClassLoader())) {
            return;
        }
        Map<String, Object> systemProperties = environment.getSystemProperties();
        String sentinelPropKey = SENTINEL_CIRCUITBREAKER_ENABLED_KEY;
        if (!ObjectUtil.isEmpty(systemProperties.get(sentinelPropKey))) {
            return;
        }
        log.info("[OneLinkFrm] 已集成ONE-LINK熔断器模块,自动关闭Sentinel原生配置: spring.cloud.circuitbreaker.sentinel.enabled=false");
        systemProperties.put(sentinelPropKey, false);
    }
}
